/*
 * HWaccess.h
 *
 * This class provides easy, threadsave access to the pins of the DIO-Board 104-DIO.
 *
 *  Created on: 11.04.2011
 *      Author: Jasper
 */

#ifndef HWACCESS_H_
#define HWACCESS_H_

#include <pthread.h>
#include <hw/inout.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/neutrino.h>
#include <unistd.h>

#include "constants.h"

#ifdef SIMULATION
#include <ioaccess.h>
#endif


/**
 * \brief Class to handle the HW-Access
 * This class is for easy and thread save HW-access to the HAW-Factory-Assembly
 * It contains access functions for the HW.
 * Also constants for these functions are given.
 */
class HWaccess {

private:
    pthread_mutex_t writeLockDIO;
    pthread_mutex_t writeLockAIO;
    static pthread_mutex_t singleton;
    static HWaccess* instance;
    int portAVal;
    int portCVal;

    /**
     * \brief Initializes HW
     *
     * \returns 0 if successful.
     */
    int init();

    /**
     * Constructor
     */
    HWaccess();

    /**
     * Destructor
     */
    virtual ~HWaccess();

public:
	/**
	 * \brief Gets singleton instance of HWaccess
	 *
     * \returns Instance of HWaccess
     */
    static HWaccess* getInstance();

    /**
     * \brief Sets given bit at given port to val.
     * Port contains bit and port, please use defined values.
     * Val is interpreted as boolean, 0=>false, ohters=>true.
     *
     * \param port the port number
     * \param val the value to which the port will be set
     *
     * \returns 0 if successful.
     */
    int set(const int port, const int val);

    /**
     * \brief Sets given bits at given ports to given values.
     * Like set(), but with multiple values and ports.
     * size has to contain the size of the arrays.
     *
     * \param portV array with the port numbers to be set
     * \param valV array with the values the ports will be set to
     * \param size the number of the ports and values
     *
     * \returns 0 if successful.
     */
    int setV(const int* portV, const int* valV, const int size);

    /**
     * \brief Gets the Value of the given bit and port into resu.
     * resu will contain 0 or 1 (for false or true).
     *
     * \param port the port number of which to get its value
     * \param resu the variable in which the result will be stored
     *
     * \returns 0 if successful.
     */
    int get(const int port, int* resu);

    /**
     * \brief Gets the Value of the given bits and ports into resuV.
     * Each element of resuV will contain 0 or 1 (for true or false).
     * size has to contain the size of the arrays.
     *
     * \param portV array with the port numbers to get their values from
     * \param valV array with the variables the results will be written to
     * \param size the number of the ports and values
     *
     * \returns 0 if successful.
     */
    int getV(const int* portV, int* valV, const int size);

    /**
     * \brief Gets the Mutex for synchronization of write to Ports
     */
    pthread_mutex_t* getWriteLockDIO();

    /**
     * \brief Gets the actual Value of the Height Sensor.
     *
     * \returns height
     */
    int getHeigthVal();
};

#endif /* HWACCESS_H_ */
